package api

import (
	"api-gateway/basic/inits"
	__ "api-gateway/basic/user_proto"
	"api-gateway/handler/request"
	"api-gateway/handler/response"
	"context"
	"github.com/gin-gonic/gin"
)

// TrustedDeviceAdd
// @Summary      信任设备添加
// @Description  信任设备添加
// @Tags         设备模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.TrustedDeviceAdd  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /trustedDevice/trustedDeviceAdd [post]
func TrustedDeviceAdd(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.TrustedDeviceAdd
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	add, err := inits.UserClient.TrustedDeviceAdd(context.Background(), &__.TrustedDeviceAddReq{
		UserId:     int64(userId),
		DeviceName: req.DeviceName,
		DeviceType: req.DeviceType,
		OsType:     req.OsType,
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "信任设备添加成功", add)

}

// TrustedDeviceDel
// @Summary      信任设备删除
// @Description  信任设备删除
// @Tags         设备模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.TrustedDeviceDel  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /trustedDevice/trustedDeviceDel [post]
func TrustedDeviceDel(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.TrustedDeviceDel
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	_, err := inits.UserClient.TrustedDeviceDel(context.Background(), &__.TrustedDeviceDelReq{
		UserId: int64(userId),
		Id:     req.Id,
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}

	response.Success(c, "信任设备删除成功", nil)

}

// TrustedDeviceUpdate
// @Summary      信任设备修改
// @Description  信任设备修改
// @Tags         设备模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.TrustedDeviceUpdate  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /trustedDevice/trustedDeviceUpdate [post]
func TrustedDeviceUpdate(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.TrustedDeviceUpdate
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.UserClient.TrustedDeviceUpdate(context.Background(), &__.TrustedDeviceUpdateReq{
		UserId:     int64(userId),
		Id:         req.Id,
		DeviceName: req.DeviceName,
		DeviceType: req.DeviceType,
		OsType:     req.OsType,
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "信任设备修改成功", nil)
}

// TrustedDeviceFind
// @Summary      信任设备展示
// @Description  信任设备展示
// @Tags         设备模块
// @Accept       json
// @Produce      json
// @Param        user  query      request.TrustedDeviceFind  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /trustedDevice/trustedDeviceFind [get]
func TrustedDeviceFind(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.TrustedDeviceFind
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	find, err := inits.UserClient.TrustedDeviceFind(context.Background(), &__.TrustedDeviceFindReq{
		UserId: int64(userId),
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "信任设备展示成功", find)
}
